package com.csh.order.config;




import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csh.order.entity.TbAdmin;
import com.csh.order.entity.TbGoods;
import com.csh.order.entity.TbLog;
import com.csh.order.entity.dto.LogGoodsAdmin;
import com.csh.order.feign.FeignClients;
import com.csh.order.mapper.TbGoodsMapper;
import com.csh.order.mapper.TbLogMapper;
import com.rabbitmq.client.Channel;

import org.springframework.amqp.core.Message;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class MessageRecv {
    @Autowired
    TbGoodsMapper tbGoodsMapper;
    @Autowired
    FeignClients feignClients;
    @Autowired
    TbLogMapper tbLogMapper;
    @RabbitListener(queues = "public_goods_num")
    public void onGoodsNum(Message message, Channel channel) throws IOException {
        byte[] body = message.getBody();
        String a= new String(body);
        LogGoodsAdmin logGoodsAdmin = JSONArray.parseObject(a, LogGoodsAdmin.class);
        String numBsf = logGoodsAdmin.getNumBsf();
        //判断用户是否已经购买过商品了
            //购买过商品直接修改商品详情就可以了
            //没有购买过就添加商品详情表
        //先要获取用户信息
        Integer adminId = logGoodsAdmin.getAdminId();
        TbAdmin tbAdmin=new TbAdmin();
        tbAdmin.setId(adminId);
        TbAdmin admins= feignClients.adminIdList(tbAdmin);
        QueryWrapper<TbLog> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(TbLog::getName,admins.getName()).eq(TbLog::getPhone,admins.getPhone()).eq(TbLog::getGoodsId,logGoodsAdmin.getGoodsId());
        //商品详情信息
        TbLog tbLog1 = tbLogMapper.selectOne(queryWrapper);
        //商品信息
        TbGoods tbGoods = tbGoodsMapper.selectById(logGoodsAdmin.getGoodsId());
        //当numBsf不为空的话证明已经有了商品详情只需要修改这个商品详情就可以了
        if(numBsf!=null){
            TbLog tbLog=new TbLog();
            tbLog.setId(tbLog1.getId());
            tbLog.setNum(tbLog1.getNum()+1);
            tbLogMapper.updateById(tbLog);
        }else{
            //没有这个商品详情 添加商品详情
            TbLog tbLog=new TbLog();
            tbLog.setName(admins.getName());
            tbLog.setPhone(admins.getPhone());
            tbLog.setNum(1);
            //查询商品

            tbLog.setGoodsId(tbGoods.getId());
            tbLog.setGoodsName(tbGoods.getGoodsName());
            //添加购买商品的详情表
            tbLogMapper.insert(tbLog);
        }

        //查询出商品的信息
        //每次只能买一个 ,修改数量
        TbGoods tbGoods1=new TbGoods();
        tbGoods1.setId(tbGoods.getId());
        tbGoods1.setGoodsCommodity(tbGoods.getGoodsCommodity()-1);
        //每次点击销售量+1
        tbGoods1.setGoodsQuantity(tbGoods.getGoodsQuantity()+1);
        //每次点击 点击率+1
        tbGoods1.setClickRate(tbGoods.getClickRate()+1);
        //修改商品
        tbGoodsMapper.updateById(tbGoods1);
        System.out.println("消息接受成功");

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}
